package api

import (
	"api-getaway/handler/request"
	"api-getaway/model"
	"github.com/gin-gonic/gin"
	"net/http"
)

func ShopAdd(r *gin.Context) {
	var req request.Shop
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}

	shop := model.Shop{
		Name:  req.Name,
		Price: req.Price,
		Stock: req.Stock,
	}

	if shop.Name != "" && shop.Price != 0 {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "名称不能为空,价格为正数",
			"data":    nil,
		})
		return
	}

	if err = shop.CreateShop(); err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "商品创建失败",
			"data":    nil,
		})
		return
	}

	r.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "商品添加成功",
		"data":    shop,
	})
}

func DisplayAdd(r *gin.Context) {
	var req request.DisplayWindow
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}

	window := model.DisplayWindow{
		Name: req.Name,
	}
	if err = window.CreateDisplayWindow(); err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "创建失败",
			"data":    nil,
		})
		return
	}
	r.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "橱窗分类添加成功",
		"data":    window,
	})
}

func ShowcaseProductsAdd(r *gin.Context) {
	var req request.ShowcaseProducts
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}

	products := model.ShowcaseProducts{
		Name:             req.Name,
		Sales:            req.Sales,
		BelongingExperts: req.BelongingExperts,
		DisplayWindowId:  req.DisplayWindowId,
		Price:            req.Price,
	}
	if err = products.CreateShowcaseProducts(); err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "创建失败",
			"data":    nil,
		})
		return
	}
	r.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "橱窗商品添加成功",
		"data":    products,
	})
}

func OrderAdd(r *gin.Context) {
	var req request.UserOrder
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "参数错误",
			"data":    nil,
		})
		return
	}
	order := model.UserOrder{
		OrderId: req.OrderId,
		ShopId:  req.ShopId,
		UserId:  req.UserId,
		Time:    req.Time,
		Status:  req.Status,
	}
	if err = order.CreateUserOrder(); err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "校验失败",
			"data":    nil,
		})
		return
	}
	r.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "校验成功",
		"data":    order,
	})
}
